package com.arsn.myf.system.controller;

import com.arsn.myf.common.exception.CustomException;
import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.system.entity.Menu;
import com.arsn.myf.system.service.MenuService;
import com.arsn.myf.system.vo.MenuVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wildfly.common.annotation.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 菜单
 * @author zhongrj
 * @since 2022-07-10
 */
@RestController
@RequestMapping("/menu")
@AllArgsConstructor
@Api(value = "菜单", tags = "菜单")
public class MenuController {

    private final MenuService menuService;

    /**
     * 查询菜单的分页数据
     * @param page
     * @param menu
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询菜单的分页数据")
    public R list(Page<Menu> page, Menu menu){
        return R.data(menuService.page(page,new QueryWrapper<>(menu)));
    }


    /**
     * 自定义查询菜单的分页数据
     * @param menu
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义查询菜单的分页数据")
    public R page(MenuVO menu){
        return R.data(menuService.selectPageMenuList(menu));
    }


    /**
     * 菜单详情信息
     * @param menu
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "菜单详情信息")
    public R detail(Menu menu){
        return R.data(menuService.getOne(new QueryWrapper<>(menu)));
    }


    /**
     * 菜单新增
     * @param menu
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "菜单新增")
    public R save(@RequestBody Menu menu){
        //判断编号是否重复
        validate(menu);
        //新增并返回
        return R.status(menuService.save(menu));
    }

    /**
     * 校验路径
     * @param menu
     */
    public void validate(Menu menu){
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("path", menu.getPath());
        List<Menu> list = menuService.list(wrapper);
        if (list.size()>0){
            throw new CustomException("path已存在!");
        }
    }

    /**
     * 菜单修改
     * @param menu
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "菜单修改")
    public R update(@RequestBody Menu menu){
        //修改
        return R.status(menuService.updateById(menu));
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除,批量删除")
    public R remove(@NotNull String ids){
        return R.status(menuService.removeByIds(Func.stringToLong(Arrays.asList(ids.split(",")))));
    }

    /**
     * 登录完成获取菜单列表
     * @param menu 菜单实体
     * @return
     */
    @GetMapping("/getMenu")
    @ApiOperation(value = "登录完成获取菜单列表")
    public R getMenu(MenuVO menu){
        //查询并返回
        return R.data(menuService.getMenu(menu));
    }

    /**
     * 获取菜单树
     * @param menu 菜单查询参数对象
     * @return
     */
    @GetMapping("getMenuTree")
    @ApiOperation(value = "获取菜单树",notes = "传入菜单对象信息")
    public R getMenuTree(MenuVO menu){
        return R.data(menuService.getMenuTree(menu));
    }

    /**
     * 根据角色id 查询当前角色拥有的菜单集合id
     * @param roleId 角色id
     * @return
     */
    @GetMapping("getMenuListByRoleId")
    @ApiOperation(value = "根据角色id 查询当前角色拥有的菜单集合id",notes = "传入角色id")
    public R getMenuListByRoleId(String roleId){
        return R.data(menuService.getMenuListByRoleId(roleId));
    }
}
